//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.jeecg.chat.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jeecg.chat.entity.P3ImTSBaseUser;
import com.jeecg.chat.entity.P3ImTSDepart;
import com.jeecg.chat.service.ImService;
import com.jeecg.chat.util.ChatResourceConfig;
import com.mchange.lang.LongUtils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.VelocityContext;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.p3.core.author.LoginUser;
import org.jeecgframework.p3.core.util.plugin.ContextHolderUtils;
import org.jeecgframework.p3.core.util.plugin.ViewVelocity;
import org.jeecgframework.p3.core.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

@Controller
@RequestMapping({"/chat/imController"})
public class ImController extends BaseController {
    private Log log = LogFactory.getLog(this.getClass());
    private static final String localPath;
    @Autowired
    private ImService imService;

    static {
        ResourceBundle bundler = ResourceBundle.getBundle("chat_config");
        localPath = bundler.getString("local_path");
    }

    public ImController() {
    }

    @RequestMapping(
        method = {RequestMethod.GET, RequestMethod.POST}
    )
    public void index(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception {
        try {
            VelocityContext velocityContext = new VelocityContext();
            String viewName = "chat/index.vm";
            ViewVelocity.view(request, response, viewName, velocityContext);
        } catch (Exception var6) {
            var6.printStackTrace();
        }

    }

    @ResponseBody
    @RequestMapping(
        params = {"getMembers"},
        method = {RequestMethod.GET, RequestMethod.POST}
    )
    public JSONObject getMembers(HttpServletResponse response, HttpServletRequest request) throws Exception {
        List<P3ImTSBaseUser> imTSUsers = this.imService.getUsers();
        LoginUser u = ContextHolderUtils.getLoginSessionUser();
        JSONArray list = new JSONArray();
        Iterator var7 = imTSUsers.iterator();

        while(var7.hasNext()) {
            P3ImTSBaseUser tsBaseUser = (P3ImTSBaseUser)var7.next();
            if (this.imService.isInThisOrg(u.getDepartid(), tsBaseUser.getMaindepartid())) {
                JSONObject json = new JSONObject();
                json.put("username", tsBaseUser.getRealname());
                json.put("id", tsBaseUser.getId());
                json.put("avatar", "http://tp1.sinaimg.cn/1571889140/180/40030060651/1");
                json.put("sign", tsBaseUser.getUsername());
                list.add(json);
            }
        }

        JSONObject jsonObject = new JSONObject();
        JSONObject data = new JSONObject();
        data.put("list", list);
        jsonObject.put("data", data);
        jsonObject.put("members", list.size() + 1);
        jsonObject.put("code", "0");
        jsonObject.put("msg", "");
        return jsonObject;
    }
    
    /**
     * 读取前台用户信息
     * @param response
     * @param request
     * @param touserid
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(
        params = {"getUser"},
        method = {RequestMethod.GET, RequestMethod.POST}
    )
    public JSONObject getUser(HttpServletResponse response, HttpServletRequest request, 
    		String touserid, String type) throws Exception {
    	String basePath = "http://localhost:8080/jeecg/"; //request.getContextPath();
    	
    	P3ImTSBaseUser tsBaseUser = null;

    	if ("client".equals(type)) {
    		List<P3ImTSBaseUser> imTSUsers = this.imService.getUsersClient(new String[]{touserid});
    		
    		if (imTSUsers.size() > 0) {
    			tsBaseUser = imTSUsers.get(0);
        	}
    	} else {
    		List<P3ImTSBaseUser> imTSUsers = this.imService.getUsers();
        	
        	for (P3ImTSBaseUser item : imTSUsers) {
        		if (item.getId().equals(touserid)) {
        			tsBaseUser = item;
        		}
        	}
    	}

    	if (tsBaseUser == null) {
    		return null;
    	}
    	
    	JSONObject json = new JSONObject();
        json.put("username", tsBaseUser.getRealname());
        json.put("id", tsBaseUser.getId());
        json.put("avatar", StringUtils.isEmpty(tsBaseUser.getPortrait()) ?  basePath + "plug-in/layui/images/portrait/qq.jpg" : basePath + "/chat/imController/showOrDownByurl.do?dbPath=" + tsBaseUser.getPortrait());
        json.put("sign", StringUtils.isEmpty(tsBaseUser.getImsign()) ? "这家伙很懒没有签名" : tsBaseUser.getImsign());
    	
    	return json;
    }

    /**
     * 读取用户列表
     * @param response
     * @param request
     * @param userid
     * @param type
     * @param touserid
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(
        params = {"getUsers"},
        method = {RequestMethod.GET, RequestMethod.POST}
    )
    public JSONObject getUsers(HttpServletResponse response, HttpServletRequest request, 
    		String userid, String type, String touserid) throws Exception {
        if ("client".equals(type)) {
        	return this.getUsersClient(response, request, userid, type, touserid);
        } else {
        	return this.getUsersBackground(response, request, userid, type, touserid);
        }
    }
    
    /**
     * 读取后台用户列表
     * @param response
     * @param request
     * @param userid
     * @param type
     * @param touserid
     * @return
     * @throws Exception
     */
    private JSONObject getUsersBackground(HttpServletResponse response, HttpServletRequest request, 
    		String userid, String type, String touserid) throws Exception {
        LoginUser u = ContextHolderUtils.getLoginSessionUser();
        JSONObject jsonObject = new JSONObject();
        JSONArray groups = new JSONArray();
        JSONObject group = new JSONObject();
        List<P3ImTSBaseUser> imTSUsers = this.imService.getUsers();
        P3ImTSBaseUser imTSBaseUser = new P3ImTSBaseUser();

        for(int i = 0; i < imTSUsers.size(); ++i) {
            if (((P3ImTSBaseUser)imTSUsers.get(i)).getId().equals(u.getId())) {
                imTSBaseUser = (P3ImTSBaseUser)imTSUsers.get(i);
                imTSUsers.remove(i);
            }
        }

        jsonObject.put("code", "0");
        jsonObject.put("msg", "");
        JSONObject jsonObjectData = new JSONObject();
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("username", u.getRealName());
        jsonObject1.put("id", u.getId());
        jsonObject1.put("status", "online");
        jsonObject1.put("sign", StringUtils.isEmpty(imTSBaseUser.getImsign()) ? "这家伙很懒没有签名" : imTSBaseUser.getImsign());
        String basePath = "http://localhost:8080/jeecg/"; //request.getContextPath();
        jsonObject1.put("avatar", StringUtils.isEmpty(imTSBaseUser.getPortrait()) ? basePath + "plug-in/layui/images/portrait/qq.jpg" : basePath + "chat/imController/showOrDownByurl.do?dbPath=" + imTSBaseUser.getPortrait());
        jsonObjectData.put("mine", jsonObject1);
        JSONArray jsonArray1 = new JSONArray();
        List<P3ImTSDepart> departs = this.imService.getDeparts();
        Iterator var15 = departs.iterator();

        while(var15.hasNext()) {
            P3ImTSDepart depart = (P3ImTSDepart)var15.next();
            JSONObject jsonObject2 = new JSONObject();
            jsonObject2.put("groupname", depart.getDepartname());
            jsonObject2.put("id", depart.getId());
            JSONArray jsonArray = new JSONArray();
            if (this.imService.isInThisOrg(u.getDepartid(), depart.getOrgCode())) {
                group.put("groupname", depart.getDepartname());
                group.put("id", depart.getId());
                group.put("avatar", "plug-in/layui/images/portrait/group.jpg");
                groups.add(group);
            }

            Iterator var19 = imTSUsers.iterator();

            while(var19.hasNext()) {
                P3ImTSBaseUser tsBaseUser = (P3ImTSBaseUser)var19.next();
                if (tsBaseUser.getMaindepartid().equals(depart.getOrgCode())) {
                    JSONObject json = new JSONObject();
                    json.put("username", tsBaseUser.getRealname());
                    json.put("id", tsBaseUser.getId());
                    json.put("avatar", StringUtils.isEmpty(tsBaseUser.getPortrait()) ?  basePath + "plug-in/layui/images/portrait/qq.jpg" : basePath + "/chat/imController/showOrDownByurl.do?dbPath=" + tsBaseUser.getPortrait());
                    json.put("sign", StringUtils.isEmpty(tsBaseUser.getImsign()) ? "这家伙很懒没有签名" : tsBaseUser.getImsign());
                    jsonArray.add(json);
                }
            }

            jsonObject2.put("list", jsonArray);
            jsonArray1.add(jsonObject2);
        }

        jsonObjectData.put("friend", jsonArray1);
        jsonObjectData.put("group", groups);
        jsonObject.put("data", jsonObjectData);
        return jsonObject;
    }
    
    /**
     * 读取前台用户列表
     * @param response
     * @param request
     * @param uid
     * @return
     * @throws Exception
     */
    private JSONObject getUsersClient(HttpServletResponse response, HttpServletRequest request, 
    		String uid, String type, String touserid) throws Exception {
    	List<String> lastMsgUsersList = this.imService.getLastMsgUsers(uid);
    	List<String> removeMsgUsersList = new ArrayList();
    	
    	lastMsgUsersList.add(uid);
    	if (StringUtil.isNotEmpty(touserid)) {
    		lastMsgUsersList.add(touserid);
    	}
    	
    	for (String item : lastMsgUsersList) {
    		if (!org.apache.commons.lang3.StringUtils.isNumeric(item)) {
    			removeMsgUsersList.add(item);
    		}
    	}
    	
    	lastMsgUsersList.removeAll(removeMsgUsersList);
    	
    	String[] lastMsgUsersIds = new String[lastMsgUsersList.size()];
    	lastMsgUsersList.toArray(lastMsgUsersIds);
    	
        JSONObject jsonObject = new JSONObject();
        JSONArray groups = new JSONArray();
        JSONObject group = new JSONObject();
        List<P3ImTSBaseUser> imTSUsers = this.imService.getUsersClient(lastMsgUsersIds);
        P3ImTSBaseUser imTSBaseUser = new P3ImTSBaseUser(); 

        for(int i = 0; i < imTSUsers.size(); ++i) {
            if (((P3ImTSBaseUser)imTSUsers.get(i)).getId().equals(uid)) {
                imTSBaseUser = (P3ImTSBaseUser)imTSUsers.get(i);
                imTSUsers.remove(i);
            }
        }

        jsonObject.put("code", "0");
        jsonObject.put("msg", "");
        JSONObject jsonObjectData = new JSONObject();
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("username", imTSBaseUser.getRealname());
        jsonObject1.put("id", imTSBaseUser.getId());
        jsonObject1.put("status", "online");
        jsonObject1.put("sign", StringUtils.isEmpty(imTSBaseUser.getImsign()) ? "这家伙很懒没有签名" : imTSBaseUser.getImsign());
        String basePath = "http://localhost:8080/jeecg/"; //request.getContextPath();
        jsonObject1.put("avatar", StringUtils.isEmpty(imTSBaseUser.getPortrait()) ? basePath + "plug-in/layui/images/portrait/qq.jpg" : basePath + "chat/imController/showOrDownByurl.do?dbPath=" + imTSBaseUser.getPortrait());
        jsonObjectData.put("mine", jsonObject1);
        JSONArray jsonArray1 = new JSONArray();
        
        // 添加近期联系人
        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("groupname", "近期联系人");
        jsonObject2.put("id", "last");
        JSONArray jsonArray = new JSONArray();
        jsonArray1.add(jsonObject2);
        
        Iterator var19 = imTSUsers.iterator();
        
        while(var19.hasNext()) {
            P3ImTSBaseUser tsBaseUser = (P3ImTSBaseUser)var19.next();
            JSONObject json = new JSONObject();
            json.put("username", tsBaseUser.getRealname());
            json.put("id", tsBaseUser.getId());
            json.put("avatar", StringUtils.isEmpty(tsBaseUser.getPortrait()) ?  basePath + "plug-in/layui/images/portrait/qq.jpg" : basePath + "/chat/imController/showOrDownByurl.do?dbPath=" + tsBaseUser.getPortrait());
            json.put("sign", StringUtils.isEmpty(tsBaseUser.getImsign()) ? "这家伙很懒没有签名" : tsBaseUser.getImsign());
            jsonArray.add(json);
            
            jsonObject2.put("list", jsonArray);
            
        }

        jsonObjectData.put("friend", jsonArray1);
        jsonObjectData.put("group", groups);
        jsonObject.put("data", jsonObjectData);
        return jsonObject;
    }

    /**
     * 读取后台登录人ID
     * @return
     */
    @ResponseBody
    @RequestMapping(
        params = {"getUserid"},
        method = {RequestMethod.GET, RequestMethod.POST}
    )
    public String getUserid() {
        LoginUser u = ContextHolderUtils.getLoginSessionUser();
        System.out.println("当前登录人id为：" + u.getId());
        return u.getId();
    }

    @RequestMapping(
        params = {"uploadImage"},
        method = {RequestMethod.GET, RequestMethod.POST}
    )
    @ResponseBody
    public JSONObject uploadImage(MultipartHttpServletRequest request) {
        JSONObject resultJson = new JSONObject();

        try {
            MultipartFile uploadify = request.getFile("file");
            byte[] bytes = uploadify.getBytes();
            String realFilename = uploadify.getOriginalFilename();
            String fileExtension = realFilename.substring(realFilename.lastIndexOf("."));
            String filename = System.currentTimeMillis() + fileExtension;
            StringBuffer path = new StringBuffer();
            path.append(localPath);
            path.append(File.separator);
            StringBuffer dbPath = new StringBuffer();
            dbPath.append("chat");
            dbPath.append(File.separator);
            dbPath.append("uploadImage");
            dbPath.append((new SimpleDateFormat(File.separator + "YYYY" + File.separator + "MM" + File.separator + "dd")).format(new Date()));
            path.append(dbPath.toString());
            String uploadDir = path.toString();
            File dirPath = new File(uploadDir);
            if (!dirPath.exists()) {
                dirPath.mkdirs();
            }

            File uploadedFile = new File(uploadDir + File.separator + filename);
            FileCopyUtils.copy(bytes, uploadedFile);
            JSONObject jsonObject = new JSONObject();
            String basePath = "http://localhost:8080/jeecg/"; //request.getContextPath();
            jsonObject.put("src", basePath + "chat/imController/showOrDownByurl.do?dbPath=" + dbPath.toString() + "/" + filename);
            this.log.info("上传路径为=" + jsonObject.get("src"));
            jsonObject.put("name", filename);
            resultJson.put("code", 0);
            resultJson.put("msg", "success");
            resultJson.put("data", jsonObject);
        } catch (Exception var15) {
            this.log.debug(var15.getMessage());
            resultJson.put("code", 1);
            resultJson.put("msg", "上传图片失败：" + var15.getMessage());
        }

        return resultJson;
    }

    @ResponseBody
    @RequestMapping(
        params = {"uploadFile"}
    )
    public JSONObject uploadFile(MultipartHttpServletRequest request) {
        JSONObject resultJson = new JSONObject();

        try {
            MultipartFile uploadify = request.getFile("file");
            byte[] bytes = uploadify.getBytes();
            String realFilename = uploadify.getOriginalFilename();
            String fileExtension = realFilename.substring(realFilename.lastIndexOf("."));
            String filename = System.currentTimeMillis() + fileExtension;
            StringBuffer path = new StringBuffer();
            path.append(localPath);
            path.append(File.separator);
            StringBuffer dbPath = new StringBuffer();
            dbPath.append("chat");
            dbPath.append(File.separator);
            dbPath.append("uploadFile");
            dbPath.append((new SimpleDateFormat(File.separator + "YYYY" + File.separator + "MM" + File.separator + "dd")).format(new Date()));
            path.append(dbPath);
            String uploadDir = path.toString();
            File dirPath = new File(uploadDir);
            if (!dirPath.exists()) {
                dirPath.mkdirs();
            }

            File uploadedFile = new File(uploadDir + File.separator + filename);
            FileCopyUtils.copy(bytes, uploadedFile);
            (new StringBuilder(String.valueOf(ChatResourceConfig.getDomain()))).append("/").append(path.toString().replace(File.separator, "/")).append("/").append(filename).toString();
            JSONObject jsonObject = new JSONObject();
            String basePath = "http://localhost:8080/jeecg/";//request.getContextPath();
            jsonObject.put("src", basePath + "chat/imController/showOrDownByurl.do?down=1&dbPath=" + dbPath.toString() + "/" + filename);
            jsonObject.put("name", realFilename);
            resultJson.put("code", 0);
            resultJson.put("msg", "success");
            resultJson.put("data", jsonObject);
        } catch (Exception var16) {
            this.log.debug(var16.getMessage());
            resultJson.put("code", 1);
            resultJson.put("msg", "上传文件失败：" + var16.getMessage());
        }

        return resultJson;
    }

    @RequestMapping(
        value = {"showOrDownByurl"},
        method = {RequestMethod.GET}
    )
    public void getImgByurl(HttpServletResponse response, HttpServletRequest request) throws Exception {
        String flag = request.getParameter("down");
        String dbpath = request.getParameter("dbPath");
        if ("1".equals(flag)) {
            response.setContentType("application/x-msdownload;charset=utf-8");
            String fileName = dbpath.substring(dbpath.lastIndexOf(File.separator) + 1);
            response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1"));
        } else {
            response.setContentType("image/jpeg;charset=utf-8");
        }

        InputStream inputStream = null;
        ServletOutputStream outputStream = null;

        try {
            String imgurl = localPath + File.separator + dbpath;
            inputStream = new BufferedInputStream(new FileInputStream(imgurl));
            outputStream = response.getOutputStream();
            byte[] buf = new byte[1024];

            int len;
            while((len = inputStream.read(buf)) > 0) {
                outputStream.write(buf, 0, len);
            }

            response.flushBuffer();
        } catch (Exception var13) {
            this.log.info("--通过流的方式获取文件异常--" + var13.getMessage());
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }

            if (outputStream != null) {
                outputStream.close();
            }

        }
    }

    @ResponseBody
    @RequestMapping(
        params = {"changeSign"}
    )
    public String changeSign(String sign) {
        LoginUser u = ContextHolderUtils.getLoginSessionUser();
        this.imService.updateSign(sign, u.getId());
        return "success";
    }
}
